package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Community;
import com.atguigu.entity.Dict;
import com.atguigu.service.CommunityService;
import com.atguigu.service.DictService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @PACKAGE_NAME: com.atguigu.controller
 * @CLASSNAME: CommunityController
 * @AUTHOR: zhangsan
 * @DATE: 2024/4/21 11:58
 * @SINCE 17.0.7
 * @DESCRIPTION: CommunityController
 */
@Slf4j
@Controller
@RequestMapping("/community")
public class CommunityController extends BaseController {
    @Reference
    private CommunityService communityService;

    @Reference
    private DictService dictService;

    private static final String PAGE_INDEX = "community/index";
    private static final String PAGE_CREATE = "community/create";
    private static final String PAGE_EDIT = "community/edit";
    private static final String LIST_ACTION = "redirect:/community";

    private static final String PAGE_SUCCESS = "common/successPage";

    /**
     * 首页分页,通过单表多次查询实现
     * @param model
     * @param request
     * @return
     */
    public String index2(Model model, HttpServletRequest request) {
        Map<String, Object> filters = super.getFilters(request);
        List<Dict> areaList = dictService.findListByDictCode("beijing");
        PageInfo<Community> page = communityService.findPage(filters);
        List<Community> communityList = page.getList();

        //todo 用stream流的 map改造
        Iterator<Community> iterator = communityList.iterator();
        while (iterator.hasNext()) {
            Community community = iterator.next();
            String areaName = dictService.getNameById(community.getAreaId());
            String plateName = dictService.getNameById(community.getPlateId());
            community.setAreaName(areaName);
            community.setPlateName(plateName);
        }

        model.addAttribute("areaList", areaList);
        model.addAttribute("page", page);
        model.addAttribute("filters", filters);
        return PAGE_INDEX;
    }

    /**
     * 首页分页,通过多表联查实现
     * @param model
     * @param request
     * @return
     */
    @RequestMapping
    public String index(@NonNull Model model, HttpServletRequest request) {
        Map<String, Object> filters = super.getFilters(request);
        List<Dict> areaList = dictService.findListByDictCode("beijing");
        PageInfo<Community> page = communityService.findPage(filters);

        model.addAttribute("areaList", areaList);
        model.addAttribute("page", page);
        model.addAttribute("filters", filters);
        return PAGE_INDEX;
    }

    @RequestMapping("/create")
    public String toCreatePage(Model model) {
        List<Dict> dictList = dictService.findListByDictCode("beijing");
        model.addAttribute("areaList", dictList);
        return PAGE_CREATE;
    }

    @RequestMapping("/save")
    public String save(@NonNull Community community) {
        Integer i = communityService.insert(community);
        return PAGE_SUCCESS;
    }

    @RequestMapping("/edit/{id}")
    public String toEditPage(@NonNull Model model, @PathVariable("id") Long id) {
        log.debug("id = {}",id);
        Community community = communityService.getById(id);
        List<Dict> dictList = dictService.findListByDictCode("beijing");
        model.addAttribute("areaList", dictList);
        model.addAttribute("community", community);
        return PAGE_EDIT;
    }

    @RequestMapping("/update")
    public String updateCommunity(@NonNull Community community) {
        Integer update = communityService.update(community);
        return PAGE_SUCCESS;
    }

    @RequestMapping("/delete/{id}")
    public String deleteCommunity(@NonNull@PathVariable("id") Long id) {
        Integer delete = communityService.delete(id);
        return LIST_ACTION;
    }
}
